// Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.

// 获取origin信息
package origin

import (
	"encoding/json"
	"fleetmanager/api/errors"
	"fleetmanager/api/model/user"
	"fleetmanager/db/dao"
	"fleetmanager/logger"
	"fleetmanager/setting"
	"net/http"
	"strings"

	"github.com/beego/beego/v2/server/web/context"
)

type OriginService struct {
	ctx    *context.Context
	logger *logger.FMLogger
}

func NewOriginService(ctx *context.Context, logger *logger.FMLogger) *OriginService {
	s := &OriginService{
		ctx:    ctx,
		logger: logger,
	}
	return s
}
func (s *OriginService) List(userId string) (code int, rsp []byte, e *errors.CodedError) {
	s.logger.Info("receive a request to list origin info")
	queryParams := make(map[string]string)
	queryParams["user_id"] = userId
	resConf, errNew := dao.GetAllResConfigByMap(queryParams)
	if errNew != nil {
		return 500, nil, errors.NewError(errors.ServerInternalError)
	}
	totalCount := len(resConf)
	queryParams["region"] = s.ctx.Input.Query("region")
	resConf, errNew = dao.GetAllResConfigByMap(queryParams)
	if errNew != nil {
		return 500, nil, errors.NewError(errors.ServerInternalError)
	}
	var originDomainIds []user.OriginInfoResponse
	originResp := &user.ListOriginInfoResponse{
		TotalCount: totalCount, // 该用户下支持的所有Domain所有区域的总数量
		Count:      0,          // 该用户下domain用户数量
		Origins:    originDomainIds,
	}
	for _, res := range resConf {
		GenerateByUserConf(&res, originResp)
	}
	originResp.Count = len(originResp.Origins)
	rsp, errNew = json.Marshal(originResp)
	if errNew != nil {
		s.logger.Error("marshal response error: %v, rsp: %s", errNew, originResp)
		return 500, nil, errors.NewError(errors.ServerInternalError)
	}
	return http.StatusOK, rsp, nil
}

func (s *OriginService) ListRegions() (code int, rsp []byte, e *errors.CodedError) {
	supportRegions := setting.SupportRegions
	listRegions := strings.Split(supportRegions, ",")
	if len(listRegions) <= 0 {
		s.logger.Error("support regions is not existed")
		return 0, nil, errors.NewErrorF(errors.ServerInternalError, "support regions is not existed")
	}
	listRegionsRsp := &user.ListRegions{
		Count:   len(listRegions),
		Regions: listRegions,
	}
	rsp, err := json.Marshal(listRegionsRsp)
	if err != nil {
		s.logger.Error("marshal response error: %v, rsp: %s", err, listRegionsRsp)
		return 500, nil, errors.NewError(errors.ServerInternalError)
	}
	return 200, rsp, nil
}

func (s *OriginService) CheckUser() (string, *errors.CodedError) {
	user := s.ctx.Input.Query("username")
	if user != "" {
		return user, nil
	}
	return user, errors.NewError(errors.UserNotFound)
}

func GenerateByUserConf(res *dao.UserResConf, resp *user.ListOriginInfoResponse) {
	originInfo := user.OriginInfoResponse{
		DomainName:   res.DomainName,
		ProjectId:    res.ProjectId,
		Region:       res.Region,
		OriProjectId: res.OriProjectId,
	}
	resp.Origins = append(resp.Origins, originInfo)
}
